//
// Copyright (C) 2011 Solar Technology Ltd,
// All rights reserved.
//
// Author: Finux Chen <finuxchen@gmail.com>
//
// This file is part of customized widgets for touch screen.
//
#include "touchkeyboard.h"
#include <QtGui>

TouchKeyboard::TouchKeyboard(QWidget *theCaller, QWidget *parent)
	: QWidget(parent),
	caller(theCaller)
{
	keysLayout = new QGridLayout;	

	char keys_char[] = {
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p',
		'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', '-',
		'!', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '<', '.', -1 };

	QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
	QPushButton *button = 0;
	int row = 0;
	int col = 0;
	int char_index = 0;
	QString key_string;
	while(keys_char[char_index] != -1){
		switch(keys_char[char_index]){
			case '!':
				key_string = "CAPS";
				break;
			case '<':
				key_string = "<-";
				break;
			case '.':
				key_string = "RT";
				break;
			default:
				key_string = keys_char[char_index];
				break;
		}

		button = new QPushButton(key_string);
		button->setSizePolicy(sizePolicy);	
		row = char_index / 10;
		col = char_index % 10;
		keysLayout->addWidget(button, row, col);

		// for memory management and signal/slots
		button->setFocusPolicy(Qt::NoFocus);
		keyButtons.append(button);
		connect(button, SIGNAL(pressed()), this, SLOT(captureKeysInput()));

		++char_index;
	}

	setFocusPolicy(Qt::NoFocus);
	
	setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
	setLayout(keysLayout);
	setFixedSize(800, 250);
}


TouchKeyboard::~TouchKeyboard()
{
	for(int index = 0; index < keyButtons.count(); ++index){
		delete keyButtons[index];
	}
	delete keysLayout;
}

void TouchKeyboard::captureKeysInput()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	int key = 0;
	QString text;

	if(button->text() == "RT"){
		key = Qt::Key_Return;
	}else if(button->text() == "CAPS"){
		key = Qt::Key_CapsLock;
		capsLockTriggered();
	}else if(button->text() == "<-"){
		key = Qt::Key_Backspace;
	}else if(button->text() == "-"){
		key = Qt::Key_Minus;
		text = button->text();
	}else{
		char ch = button->text().at(0).toAscii();
		if('a' <= ch && ch <= 'z'){
			key = Qt::Key_A + (ch - 'a');
		}else{
			key = Qt::Key_0 + (ch - '0');
		}
		text = button->text();
	}

	QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, key, Qt::NoModifier, text);
	QCoreApplication::sendEvent(caller, event);
}


void TouchKeyboard::capsLockTriggered()
{
	QString button_text;
	for(int key_index = 0; key_index < keyButtons.count(); ++key_index){
		button_text = keyButtons[key_index]->text();
		if(button_text.length() == 1){
			if('a' <= button_text.at(0).toAscii() && 
					button_text.at(0).toAscii() <= 'z'){
				keyButtons[key_index]->setText(QChar(button_text.at(0).toAscii() - 32));
			}else if('A' <= button_text.at(0).toAscii() && 
					button_text.at(0).toAscii() <= 'Z'){
				keyButtons[key_index]->setText(QChar(button_text.at(0).toAscii() + 32));
			}
		}
	}
}
